import React from 'react';
import './app.css';
import { BrowserRouter as Router, Route } from 'react-router-dom'
import Tools from '../tools/tools.js'
import Tables from '../tables/tables.js'
import Login from '../login/login.js'
import User from '../user/user.js'
// import Activity from '../activity/activity.js'
import Service from '../service/service.js'
import AppNavigator from './appNavigator.js'
import weAjax from '../utils/ajax.js'

/**
 * 检测Token授权是否过期
 * 
 */
const checkAutho = (cb) => {
  weAjax({ method: 'post', url: "/admin/checkAutho", data: { token: localStorage.getItem('Authorization') } }, cb)
  // localStorage.clear();
}


class Home extends React.Component {

  constructor(props) {
    super(props)

    this.state = { isLogin: true, userinfo: null };
    this.login();
  }

  componentDidMount() {
    // 进入页面即开始判断是否登陆过
  }


  login = () => {

    // checkAutho(function (res) {
    //   if (res.code) {
    //     this.setState({ isLogin: true, userinfo: res.userinfo });
    //   }
    // }.bind(this))
  }

  logout = () => {
    localStorage.clear();
    this.setState({ isLogin: false, userinfo: null });
  }

  render() {
    let login = null;
    if (!this.state.isLogin) {
      login = <Login onLogin={this.login} />;
    }

    return (
      <AppNavigator isLogin={this.state.isLogin} userinfo={this.state.userinfo} onLogout={this.logout} />
    )
  }
}

/*------------- 路由配置 -------------*/
const routes = [
  {
    path: '/',
    component: Home
  },
  {
    path: '/tools',
    component: Tools
  },
  {
    path: '/tables',
    component: Tables
  },
  {
    path: '/user',
    component: User
  }
  // {
  //   path: '/activity',
  //   component: Activity
  //   // data: ['http://bm.juzhen.net/activity']
  // },
  // {
  //   path: '/service',
  //   component: Service
  // }
]



const FadingRoute = ({ component: Component, ...rest }) => (
  <Route {...rest} render={props => (

    <Component {...props} />

  )} />
)

const App = () => (
  <Router basename="/bm">
    <div>

      {/*------------- 路由 -------------*/}
      {routes.map((route, i) => (
        <FadingRoute key={i} component={route.component} {...route} />
      ))}

    </div>
  </Router>
)


export default App;
